#!/bin/sh
sleep 20s
REDIS_HOST=$1
REDIS_PASSWORD=$2
DB_HOST_IP=$3
DB_PWD=$4
ACCESS_KEY=$5
SECRET_KEY=$6
OBS_BUCKETNAME=$7
DAC_SDK_URL=$8

#下载go
cd /usr/local/ || exit
while [ ! -f "/usr/local/go1.18.4.linux-amd64.tar.gz" ]
do
	wget -c https://documentation-samples.obs.cn-north-4.myhuaweicloud.com/solution-as-code-publicbucket/solution-as-code-moudle/build-a-digital-assets-platform-based-on-MetaTown/open-source-software/go1.18.4.linux-amd64.tar.gz --no-check-certificate
done
tar -C /usr/local/ -zxvf go1.18.4.linux-amd64.tar.gz

cat > /etc/profile.d/go.sh <<- EOF
export PATH=\$PATH:/usr/local/go/bin
export GOPATH=/root/go
EOF

source /etc/profile.d/go.sh

# 下载openssl
cd /usr/local/include/ || exit
while [ ! -f "/usr/local/include/opensslv2.zip" ]
do
	wget https://bcs.obs.cn-north-1.myhuaweicloud.com/dac/opensslv2.zip
done
unzip opensslv2.zip

cat >> /etc/profile << EOF
export CGO_LDFLAGS=-L/usr/local/include/openssl
export C_INCLUDE_PATH=/usr/local/include/openssl
export LD_LIBRARY_PATH=/usr/local/include/openssl
export GONOSUMDB=*
export GOPROXY=https://repo.huaweicloud.com/repository/goproxy/,https://mirrors.tools.huawei.com/goproxy/
export GO111MODULE=on
EOF
source /etc/profile

mkdir /conf/
cd /conf/ || exit
# 下载dac配置文件
while [ ! -f "/conf/conf.yaml" ]
do
	wget ${DAC_SDK_URL}
	unzip *.zip
done

#下载后端代码
yum -y install git
cd /usr/local || exit

while [ ! -d "/usr/local/huaweicloud-solution-nft-platform-backend/" ]
do
	git clone https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-nft-platform-backend.git
done

#redis配置 app.conf
cat > /usr/local/huaweicloud-solution-nft-platform-backend/src/conf/app.conf <<- EOF
appname = nft-server
runmode = "prod"
autorender = false
recoverpanic = false
enableerrorsrender = false
copyrequestbody = true


##redis连接信息
redisHost = "${REDIS_HOST}"
redisPort = "6379"
redisPassword = "${REDIS_PASSWORD}"
redisDB = 0
redisPrefix = "NFT_"

# session配置
sessionon = true
sessionname = "sessionId"
SessionCookieLifeTime = 3600
sessiongcmaxlifetime = 3600
## 设置session存储引擎
#sessionprovider = "redis"
## redis存储引擎配置
## redis配置格式: redis地址,redis连接池最大连接数,redis密码
## redis连接池和redis密码配置，没有保持为空，例子: 127.0.0.1:6379
#sessionproviderconfig = "${REDIS_HOST}:6379,1000,${REDIS_PASSWORD}"

jwtkey = 61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o
# jwtToken的存活时间单位为分钟
jwtTokenLifeTimeMinute = 60
# for develop

# 使用http
EnableHTTP = true
EnableHTTPS = false
EnableHttpTLS = false
HTTPPort = 81
# 使用https
#EnableHTTP = false
#EnableHTTPS = true
#EnableHttpTLS = true
#HTTPSAddr = "0.0.0.0"
#HTTPSPort = 443

# 可领取的藏品数量 初始为1 可修改
availableQuantity = 1
EOF
#Mysql数据库&DAC&OBS配置config.ini
cat > /usr/local/huaweicloud-solution-nft-platform-backend/src/conf/config.ini <<- EOF
[DBConfig]
DB_VIP='${DB_HOST_IP}'
DB_PORT='3306'
DB_USER='root'
DB_PWD='${DB_PWD}'

[OBSConfig]
OBS_ENDPOINT = 'https://obs.cn-north-4.myhuaweicloud.com'
OBS_AK = '${ACCESS_KEY}'
OBS_SK = '${SECRET_KEY}'
OBS_BUCKETNAME = '${OBS_BUCKETNAME}'

[IVSConfig]
IVS_ENDPOINT = 'https://ivs.cn-north-4.myhuaweicloud.com'
IVS_AK = '${ACCESS_KEY}'
IVS_SK = '${SECRET_KEY}'

[DACConfig]A
DAC_CONFIG_PATH='/conf/conf.yaml'

[AppConfig]
AlphaMode=false

[SavePicturePath]
SavePath='img'
EOF

#下载dac go sdk
cd /usr/local || exit
while [ ! -f "/usr/local/GoSample1.3.2.tar.gz" ]
do
	wget https://digital-asset-chain.obs.cn-north-4.myhuaweicloud.com/release/version/GoSample1.3.2.tar.gz
done
tar -zxvf GoSample1.3.2.tar.gz
cp -r /usr/local/demo/tdc-go-sdk/  /usr/local/huaweicloud-solution-nft-platform-backend/

cd /usr/local/huaweicloud-solution-nft-platform-backend/src/ || exit
sed -i '/huaweicloud.com\/cce\/commons/d' go.mod
sed -i '/code.huawei.com\/bcs\/etcd/d' go.mod
go mod tidy 
#提升权限
go build -o nft-server && echo "go build----------------"
chmod +x nft-server && ./nft-server
cat > /etc/init.d/go.sh <<- EOF
#!/bin/bash
# chkconfig: 2345 10 90
# description: myservice ....
cd /usr/local/huaweicloud-solution-nft-platform-backend/src/
go build
chmod +x nft-server && ./nft-server
EOF
cd /etc/init.d || exit
chmod +x go.sh
chkconfig --add go.sh
chkconfig go.sh on